from rest_framework import serializers

from users.models import *

# 标签序列化器
class LabelSer(serializers.ModelSerializer):

    class Meta:
        model = LabelModel
        fields = '__all__'
        read_only_fields = ['id']

# 分类序列化器
class ClassifySer(serializers.ModelSerializer):

    class Meta:
        model = ClassIfyModel
        fields = '__all__'
        read_only_fields = ['id']

# 学习内容序列化器
class ContentSer(serializers.ModelSerializer):

    classify = serializers.PrimaryKeyRelatedField(queryset=ClassIfyModel.objects.all())
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())

    classify_name = serializers.SerializerMethodField()
    user_account = serializers.SerializerMethodField()
    def get_classify_name(self, obj):
        return obj.classify.name
    def get_user_account(self, obj):
        return obj.user.account

    class Meta:
        model = ContentsModel
        fields = '__all__'
        read_only_fields = ['id']

# 学习任务序列化器
class TaskSer(serializers.ModelSerializer):

    clbum = serializers.PrimaryKeyRelatedField(queryset=ClbumModel.objects.all())
    content = serializers.PrimaryKeyRelatedField(queryset=ContentsModel.objects.all())

    clbum_name = serializers.SerializerMethodField()
    content_title = serializers.SerializerMethodField()

    def get_clbum_name(self, obj):
        return obj.clbum.name
    def get_content_title(self, obj):
        return obj.content.title

    class Meta:
        model = TaskModel
        fields = '__all__'
        read_only_fields = ['id']

# 学习记录序列化器
class RecordSer(serializers.ModelSerializer):
    is_status = serializers.ChoiceField(choices=RecordModel.ORDER_STATE, source="get_is_status_display", read_only=True)

    task = serializers.PrimaryKeyRelatedField(queryset=TaskModel.objects.all())
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())

    user_account = serializers.SerializerMethodField()
    def get_user_account(self, obj):
        return obj.user.account

    class Meta:
        model = RecordModel
        fields = '__all__'
        read_only_fields = ['id']

# 弹幕序列化器
class BarrageSer(serializers.ModelSerializer):

    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    content = serializers.PrimaryKeyRelatedField(queryset=ContentsModel.objects.all())

    user_account = serializers.SerializerMethodField()
    content_title = serializers.SerializerMethodField()
    def get_user_account(self, obj):
        return obj.user.account
    def get_content_title(self, obj):
        return obj.content.title

    class Meta:
        model = BarrageModel
        fields = '__all__'
        read_only_fields = ['id']

# 学习资源序列化器
class ResourceSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    content = serializers.PrimaryKeyRelatedField(queryset=ContentsModel.objects.all())

    user_account = serializers.SerializerMethodField()
    def get_user_account(self, obj):
        return obj.user.account

    class Meta:
        model = ResourceModel
        fields = '__all__'
        read_only_fields = ['id']

# 订单序列化器
class OrderSer(serializers.ModelSerializer):

    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    resource = serializers.PrimaryKeyRelatedField(queryset=ResourceModel.objects.all())

    user_account = serializers.SerializerMethodField()
    resource_name = serializers.SerializerMethodField()
    def get_user_account(self,obj):
        return obj.user.account
    def get_resource_name(self,obj):
        return obj.resource.name

    class Meta:
        model = OrderModel
        fields = '__all__'
        read_only_fields = ['id']

# 收藏序列化器
class CollectSer(serializers.ModelSerializer):

    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    resource = serializers.PrimaryKeyRelatedField(queryset=ResourceModel.objects.all())

    user_account = serializers.SerializerMethodField()
    resource_name = serializers.SerializerMethodField()
    def get_user_account(self, obj):
        return obj.user.account
    def get_resource_name(self, obj):
        return obj.resource.name

    class Meta:
        model = CollectModel
        fields = '__all__'
        read_only_fields = ['id']